package com.ruoyi.web.controller.api.wx;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.WxModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zzp
 * @version 1.0.0
 * @ClassName IndexController.java
 * @Description 微信接口
 * @createTime 2022年10月09日 16:05:00
 */
@Api("获取微信接口")
@RestController
@RequestMapping("/api/wx")
public class WxController extends BaseController {

    @Autowired
    private TokenService tokenService;

    @ApiOperation("获取微信accessToken")
    @GetMapping("/wxAccessToken")
    public AjaxResult wxAccessToken() {
        String accessToken = tokenService.getAccessToken();
        return AjaxResult.success("操作成功", accessToken);
    }

    @ApiOperation("获取微信小程序码")
    @GetMapping("/getWxCode")
    public AjaxResult getWxCode(WxModel wxModel) {
        String accessToken = tokenService.getAccessToken();
        wxModel.setEnv_version("release");
        String base64Img = HttpUtils.getWxCode(accessToken, wxModel.getScene(), wxModel.getPage(), wxModel.getEnv_version());
        if (StringUtils.isNullOrEmpty(base64Img)) {
            return AjaxResult.error("生成小程序码失败");
        } else {
            return AjaxResult.success("操作成功", base64Img);
        }
    }
}
